package com.dk.mydemo.utils;

import org.apache.commons.lang3.StringUtils;


public class HttpEscapeUtil {
    private static final String DEFAULT_ENCODE = "utf8";

    public HttpEscapeUtil() {
    }

    public static String truncateStrByByte(String str, int length, boolean addShenglu) {
        if(addShenglu) {
            --length;
        }

        try {
            if(str == null) {
                return "";
            } else if(str.getBytes("utf8").length <= length) {
                return str;
            } else {
                int e = 0;

                int i;
                for(i = 0; i < str.length(); ++i) {
                    String tmp = str.substring(i, i + 1);
                    e += tmp.getBytes("utf8").length;
                    if(e >= length) {
                        break;
                    }
                }

                return addShenglu?str.substring(0, i) + "...":(e == length?str.substring(0, i + 1):str.substring(0, i));
            }
        } catch (Exception var6) {
            return "";
        }
    }

    public static final String js(String content) {
        return escapeJs(content);
    }

    private static final String escapeJs(String content) {
        if(org.apache.commons.lang3.StringUtils.isBlank(content)) {
            return "";
        } else {
            content = content.replace("\\", "\\\\");
            content = content.replace("\'", "\\\'");
            content = content.replace("\"", "\\\"");
            content = content.replace("/", "\\/");
            content = content.replace("\b", "\\b");
            content = content.replace("\f", "\\f");
            content = content.replace("\n", "\\n");
            content = content.replace("\r", "\\r");
            content = content.replace("\t", "\\t");
            return content;
        }
    }

    public static final String html(String content) {
        return escapeHtml(content);
    }

    private static final String escapeHtml(String content) {
        if(StringUtils.isBlank(content)) {
            return "";
        } else {
            content = content.replace("<", "&#60;");
            content = content.replace(">", "&#62;");
            content = content.replace("\"", "&#34;");
            content = content.replace("\'", "&#39;");
            content = content.replaceAll("&(?![\\w#]+;|#\\d+)", "&#38;");
            return content;
        }
    }
}
